<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    <meta charset="UTF-8">
    <title>Sucha's Blog - Archive for September, 2013</title>
    <meta name="generator" content="MarkdownProjectCompositor.lua">
    <meta name="author" content="Sucha">
    <meta name="keywords" content="suchang, programming, Linux, Lua">
    <meta name="description" content="Sucha's blog">
    <link rel="shortcut icon" href="../images/ico.png">
    <link rel="stylesheet" type="text/css" href="../styles/blog.css">
    <link rel="stylesheet" type="text/css" href="../styles/prism.min.css">
    <style id="site_theme"></style>
  </head>
  <body>
    <div id="body">
      <div id="text">
	   <!-- Page published by cmark-gfm begins here --><h1>Sucha's Blog ~ Archive for September, 2013</h1>
<p><a id="p0"></a></p>
<div class="date">13年9月29日 周日 18:45</div>
<h2>Bash 下对比两个文件夹的文件并更新</h2>
<p>有时候确实有这样的需求，两个文件夹下面的文件都差不多，但是他们因为各种原
因不属于同一个 git 根，至少在没有这些强大的版本控制系统下，我们又需要做对
比更新，那请看下面，需要 diff、cut 以及 bash 内建的 cp 命令。</p>
<pre><code class="language-bash">checknew() {
	if [ $# == 2 ]; then
		diff -rqX ~/bin/diff_ignore.txt $1 $2
	else
		echo checknew DIR1 DIR2
	fi
}
</code></pre>
<p>checknew 用来检测那些文件有变动，其中会排除一些文件，文件名的模式放到了
~/bin/diff_ignore.txt 中，我的那个内容如下：</p>
<pre><code class="language-source">*.git
*.so
*.dSYM
TAGS
*.elc
.gitignore
</code></pre>
<p>而 copynew src dest 这是将不同目录下的文件比较后做拷贝更新。</p>
<pre><code class="language-bash">copynew() {
	if [ $# != 2 ]; then
		echo copynew srcDir desDir
		return
	fi

	tmp_file=/tmp/ccc.txt
	
	# generate diff output
	diff -rqX ~/bin/diff_ignore.txt $1 $2 | grep &quot;^Files&quot; | cut -d&quot; &quot; -f 2,4 &gt; $tmp_file

	#update file
	magic=12343188
	src=$magic
	for file in $(cat $tmp_file); do
		des=$file
		if [ $src == $magic ]; then
			src=$des
            continue
		else
			cp $src $des
			src=$magic
		fi
	done

	rm $tmp_file
}
</code></pre>
<div class="category"><a href="CategoryLinux.html">CategoryLinux</a> / <a href="2013-09.html#p0">Permalink</a> / <a href="https://github.com/lalawue/homepage/discussions/categories/blog" target="_blank">Discussion</a></div>
<!-- date: 2013-09-29T18:45:56+0800 -->
<!-- Page published by cmark-gfm ends here -->
  <div id="foot">2004-<script>var d = new
	Date();document.write(d.getFullYear())</script> &copy;
	Sucha. Powered by MarkdownProjectCompositor.
  </div>
  </div><!-- text -->
  <div id="sidebar">
  </div><!-- sidebar -->
  <script src="../js/prism.min.js" async="async"></script>
  <script src="../js/blog_sidebar.js"></script>
  </div> <!-- body -->
</body>
</html>